package org.yefei.homeworkday22.supermarketsystem;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

//业务功能
public class MemberBusiness {
	Scanner in = new Scanner(System.in);
	List<Member> list = new LinkedList<>();// 会员

	// 菜单选项
	public void menu() {
		System.out.println("****************欢迎进入沃尔玛网上超市****************");
		System.out.println("1、积分累计\t2、积分兑换\t3、查询剩余积分\t4、修改密码\t5、注册会员\t6、退出");
		System.out.println("请做出您的选择：");
	}

	// 功能选择
	public void begin() {
		while (true) {
			menu();
			String choice = in.next();
			switch (choice) {
			case "1":
				accumulativeIntegral();
				continue;
			case "2":
				creditsExcaahge();
				continue;
			case "3":
				integralQuery();
				continue;
			case "4":
				changePassword();
				continue;
			case "5":
				register();
				continue;
			case "6":
				System.out.println("欢迎您的使用，期待您下次再来！");
				break;
			default:
				System.out.println("输入有误，请重新选择！");
				continue;
			}
			break;
		}
	}

	// 积分累计
	public boolean accumulativeIntegral() {
		System.out.println("请输入您的卡号：");
		int cid = in.nextInt();
		System.out.println("请输入您的密码：");
		String psd = in.next();
		Member hasMember = hasMember(cid, psd);
		if (hasMember != null) {
			System.out.println("请输入您本次消费金额（5元为1积分）：");
			int integral = in.nextInt() / 5;
			hasMember.setIntegral(integral + hasMember.getIntegral());
			System.out.println("积分累计成功！");
			return true;
		} else {
			System.out.println("此会员卡无效！无法累计积分！");
			return false;
		}
	}

	// 积分兑换
	public boolean creditsExcaahge() {
		System.out.println("请输入您的卡号：");
		int cid = in.nextInt();
		System.out.println("请输入您的密码：");
		String psd = in.next();
		Member hasMember = hasMember(cid, psd);
		if (hasMember != null) {
			System.out.println("请输入您兑换的积分额度（100积分抵1毛）：");
			int integral = in.nextInt();
			if (integral <= hasMember.getIntegral()) {
				hasMember.setIntegral(hasMember.getIntegral() - integral);
				System.out.println("您此次抵消了：" + integral / 100 * 0.1 + "元！");
			} else {
				System.out.println("积分不足！");
			}
			return true;
		} else {
			System.out.println("此会员卡无效！无法积分兑换！");
			return false;
		}
	}

	// 积分查询
	public void integralQuery() {
		System.out.println("请输入您的卡号：");
		int cid = in.nextInt();
		System.out.println("请输入您的密码：");
		String psd = in.next();
		Member hasMember = hasMember(cid, psd);
		if (hasMember != null) {
			System.out.println(hasMember);
		} else {
			System.out.println("此会员卡无效！无法查询积分！");
		}
	}

	// 修改密码
	public boolean changePassword() {
		System.out.println("请输入您的卡号：");
		int cid = in.nextInt();
		System.out.println("请输入您的原始密码：");
		String psd = in.next();
		Member hasMember = hasMember(cid, psd);
		if (hasMember != null) {
			String psw;
			System.out.println("请输入新密码：");
			while (true) {
				psw = in.next();
				if (psw.length() < 6) {
					System.out.println("密码需大于（等于）6位，请重输：");
					continue;
				} else {
					hasMember.setPsw(psw);
					break;
				}
			}
			System.out.println("修改密码成功，新密码为：" + hasMember.getPsw());
			return true;
		} else {
			System.out.println("此会员卡不在！");
			return false;
		}

	}

	// 注册办卡
	public void register() {
		Member m = new Member();
		System.out.println("请输入注册名：");
		m.setName(in.next());
		// 随机生成卡号
		int cardId = creatId();
		m.setCardNum(cardId);
		String psw;
		System.out.println("请输入注册密码：");
		while (true) {
			psw = in.next();
			if (psw.length() < 6) {
				System.out.println("密码需大于（等于）6位，请重输：");
				continue;
			} else {
				m.setPsw(psw);
				break;
			}
		}
		// 注册默认有500分
		m.setIntegral(500);
		// 开卡日期
		Date date = new Date();
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
		String f = s.format(date);
		m.setDate(f);
		System.out.println("恭喜会员卡注册成功，商城赠送500积分，您的会员卡号是：" + cardId);
		list.add(m);
	}

	// 随机生成卡号
	public int creatId() {
		int id = (int) (Math.random() * 1000000000);
		for (Member member : list) {
			if (member.getCardNum() == id) {
				id = (int) (Math.random() * 1000000000);
			}
		}
		return id;
	}

	// 根据会员卡号查询会员是否存在。
	public Member hasMember(int cdN, String pw) {
		for (Member member : list) {
			if (member.getCardNum() == cdN && member.getPsw().equals(pw)) {
				return member;
			}
		}
		return null;
	}

}
